package cn.edu.nuc.bre.controller;

import org.activiti.engine.identity.User;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.edu.nuc.bre.dto.UserInfo;
import cn.edu.nuc.bre.service.interfaces.UserService;

/**
 * 用户登录Action以及相关信息初始化
 * 
 * @author Mine
 * @since 2016年11月10日_下午3:01:04
 */
@Controller
@Scope("prototype")
public class UserLoginAction extends ActionSupport {
	/**
	 * 序列化id
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private UserService userService;

	private String oldpw;//旧密码
	private String newpw;//新密码
	private String repw;//重输
	
	private String tip;// 回显小消息
	// 页面传入参数封装
	private User user = new UserEntity();

	// 执行登录
	public String doLogin() throws Exception {
		UserInfo userinfo = userService.Login(user.getId(), user.getPassword());
		if (userinfo != null) {
			if(userinfo.hasGroupErr()){
				tip = "没有操作权限，请联系管理员。。。";
				return ERROR;
			}
			tip = "登录成功。。。";
			//System.out.println(userinfo.getName());
			// 放入session
			ActionContext.getContext().getSession().put("userInfo", userinfo);
			return SUCCESS;
		} else {
			if (userService.findById(user.getId()) == null) {
				tip = "登录失败。。。账户不存在";
			} else {
				tip = "登录失败。。。密码错误";
			}
			return ERROR;
		}

	}

	// 展示首页
	public String showIndex() throws Exception {
		return SUCCESS;
	}

	// 获取登录页面
	@Override
	public String execute() throws Exception {
		if (tip == null) {
			tip = "欢迎来到登录页面...";
		}
		return SUCCESS;
	}
	// 修改密码
	public String editPWD() throws Exception {
		if(!newpw.trim().equals(repw.trim())){
			tip="两次新密码输入不一致";
			return ERROR;
		}
		Object obj=(UserInfo) ServletActionContext.getRequest().getSession().getAttribute("userInfo");
		UserInfo userinfo=null; 
		if(obj!=null){
			userinfo=(UserInfo) obj;
			if(userService.editPWD(userinfo.getUser().getId(), oldpw, newpw)){
				tip="修改密码成功！！！";
				return SUCCESS;
			}else{
				tip="旧密码错误";
			}
		}else{
			tip="没有登录...";
		}
		return ERROR;
	}
	// 注销
	public String logout() throws Exception {
		ServletActionContext.getRequest().getSession().invalidate();
		tip = "注销成功...";
		return SUCCESS;
	}

	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	public String getOldpw() {
		return oldpw;
	}

	public void setOldpw(String oldpw) {
		this.oldpw = oldpw;
	}

	public String getNewpw() {
		return newpw;
	}

	public void setNewpw(String newpw) {
		this.newpw = newpw;
	}

	public String getRepw() {
		return repw;
	}

	public void setRepw(String repw) {
		this.repw = repw;
	}

}
